package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.dao.DefectDao;
import com.woniuxy.dao.StatisticAlanalysisDao;
import com.woniuxy.dao.WorkhoursDao;
import com.woniuxy.model.*;
import com.woniuxy.service.StatisticAlanalysisService;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.TimeMethodsutil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Service
public class StatisticAlanalysisServiceImpl  implements StatisticAlanalysisService {

    @Resource
    private DefectDao defectDao;
    @Resource
    private WorkhoursDao workhoursDao;
    @Override
    public List<DefectModelInfo> selectAlldefectTypeNum(String projectName, Date[] defectCreattimes) {

        String[] defectCreattime=null;
        if (defectCreattimes!=null&&defectCreattimes.length!=0){
            defectCreattime = TimeMethodsutil.getTimeToString(defectCreattimes);
        }

        List<DefectModelInfo> defectModelInfos = defectDao.selectAlldefectTypeNum(projectName,defectCreattime);

        return defectModelInfos;
    }

    @Override
    public List<DefectModelInfo> selectAlldefectseverityNum(String projectName, Date[] defectCreattimes) {

        String[] defectCreattime=null;
        if (defectCreattimes!=null&&defectCreattimes.length!=0){
            defectCreattime = TimeMethodsutil.getTimeToString(defectCreattimes);
        }

        List<DefectModelInfo> defectModelInfos = defectDao.selectAlldefectserverityNum(projectName,defectCreattime);

        return defectModelInfos;


    }

    @Override
    public List<DefectModelInfo> selectAlldefectstateNum(String projectName, Date[] defectCreattimes) {
        String[] defectCreattime=null;
        if (defectCreattimes!=null&&defectCreattimes.length!=0){
            defectCreattime = TimeMethodsutil.getTimeToString(defectCreattimes);
        }

        List<DefectModelInfo> defectModelInfos = defectDao.selectAlldefectstateNum(projectName,defectCreattime);

        return defectModelInfos;
    }


    @Override
    public List<DefectWorkhourDay> getAllDataofWorkhourDay(String projectName, Date[] defectCreattimes) {
        String[] defectCreattime=null;
        if (defectCreattimes!=null&&defectCreattimes.length!=0){
            defectCreattime = TimeMethodsutil.getTimeToString(defectCreattimes);
        }
        List<DefectWorkhourDay> workhourday = workhoursDao.getWorkhourday(projectName, defectCreattime);
        return workhourday;
    }

    @Override
    public List<DefectWorkhourDay> findWorkhourday(String projectName, Date[] defectCreattimes) {
        String[] defectCreattime=null;
        if (defectCreattimes!=null&&defectCreattimes.length!=0){
            defectCreattime = TimeMethodsutil.getTimeToString(defectCreattimes);
        }
        List<DefectWorkhourDay> workhourday = workhoursDao.getWorkhourday(projectName, defectCreattime);

        return workhourday;

    }

    @Override
    public List<DefectWorkmounthday> findWorkmounthday(String projectName, Date[] defectCreattimes) {
        String[] defectCreattime=null;
        if (defectCreattimes!=null&&defectCreattimes.length!=0){
            defectCreattime = TimeMethodsutil.getTimeToString(defectCreattimes);
        }
        List<DefectWorkmounthday> workmounthday = workhoursDao.getWorkmounthday(projectName, defectCreattime);



        Set<DefectWorkmounthday> defectWorkmounthdays = new LinkedHashSet<>();

        for (DefectWorkmounthday defectWorkmounthday:workmounthday){
            DefectWorkmounthday workmounthday1 = this.toDefectWorkmounthday(defectWorkmounthday.getProjectName(), null, null, null);
            DefectWorkmounthday defectWorkmounthday1 = this.toDefectWorkmounthday(defectWorkmounthday.getProjectName(), null, defectWorkmounthday.getWorkhoursCreatename(), null);
            DefectWorkmounthday defectWorkmounthday2 = this.toDefectWorkmounthday(defectWorkmounthday.getProjectName(), defectWorkmounthday.getWorkHour(), null, defectWorkmounthday.getMounth());
            defectWorkmounthdays.add(workmounthday1);
            defectWorkmounthdays.add(defectWorkmounthday1);
            defectWorkmounthdays.add(defectWorkmounthday2);
        }


        ArrayList<DefectWorkmounthday> list = new ArrayList<>();
        for (DefectWorkmounthday workmounthday1:defectWorkmounthdays){
            list.add(workmounthday1);
        }

        for (DefectWorkmounthday mounthday:list){


            if (mounthday.getWorkhoursCreatename()!=null&&mounthday.getProjectName()!=null){
                mounthday.setProjectName(null);
            }
            if(mounthday.getProjectName()!=null&& mounthday.getMounth()!=null){
                mounthday.setProjectName(null);
            }
            if (mounthday.getProjectName()!=null&&mounthday.getWorkhoursCreatename()!=null&&mounthday.getWorkHour()!=null){
                mounthday.setProjectName(null);
            }

        }
        return list;
    }




     public static DefectWorkmounthday  toDefectWorkmounthday(String projectName,String tWorkhoursCreatename,String mounth,String workHour){

        return new DefectWorkmounthday(projectName,tWorkhoursCreatename,mounth,workHour);

     }


}
